#include "StdAfx.h"
#include "UICheckBox.h"
#include <assert.h>
#include "UIGlobalStyle.h"
#include "UIGridImgPlus.h"
#include "UIDrawTool.h"
#include "UIMemDC.h"

CUICheckBox::CUICheckBox( HWND hWnd ): CUIControl( hWnd ), m_pImg( NULL )
{
    assert( BS_CHECKBOX == ( GetStyle() & BS_CHECKBOX ) );
}

CUICheckBox::~CUICheckBox( void )
{

}

void CUICheckBox::OnDraw( HDC hDC )
{
    RECT rcClient = {0};
    ::GetClientRect( GetHwnd(), &rcClient );

    if ( IsEnable() )
    {
        GetImage()->SetCurStatus( IMG_NORMAL );

        if ( IsHot() )
        {
            GetImage()->SetCurStatus( IMG_HOVER );
        }

        if ( IsChecked() )
        {
            GetImage()->SetCurStatus( IMG_PRESS );
        }

    }
    else
    {
        if ( IsChecked() )
        {
            GetImage()->SetCurStatus( 7 );

        }
        else
            GetImage()->SetCurStatus( 3 );

    }

    int y = ( rcClient.bottom - GetImage()->GetSize().cy ) / 2;
    GetImage()->SetPosition( 0, y );
    GetImage()->Draw( hDC );

// 	if (IsFocused())
// 	{
// 		RECT rcFocus={0};
// 		::CopyRect(&rcFocus,&rcClient);
// 		rcFocus.left=GetImg()->GetSize().cx+1;
// 		::DrawFocusRect(hDC,&rcFocus);
// 	}

    TCHAR sText[50] = {0};
    int iLen =::GetWindowText( GetHwnd(), sText, 50 );
    ::SetBkMode( hDC, TRANSPARENT );
    rcClient.left = GetImage()->GetSize().cx + 1;
    HFONT hOldFont = ( HFONT )::SelectObject( hDC, CUIGlobalStyle::GetFont() );
    ::SetTextColor( hDC, GetTextColor() );
    ::DrawText( hDC, sText, iLen, &rcClient, DT_VCENTER | DT_SINGLELINE | DT_CENTER );
    ::SelectObject( hDC, hOldFont );
}

LRESULT CUICheckBox::ProcessUIMsg( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    switch ( uMsg )
    {
    case BM_SETSTATE:
    {
        CUIControl::ProcessUIMsg( uMsg, wParam, lParam );
        Redraw();
        return 0;
    }
    }

    return CUIControl::ProcessUIMsg( uMsg, wParam, lParam );
}
